package com.jinglvhuanjing.app.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.jinglvhuanjing.app.R;

import java.util.ArrayList;
import java.util.HashMap;

public class ParamSettingSpinnerView extends androidx.constraintlayout.widget.ConstraintLayout {

    public ParamSettingSpinnerView(@NonNull Context context) {
        super(context);
        this.init(context);
    }

    public ParamSettingSpinnerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.init(context);
    }

    public ParamSettingSpinnerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init(context);
    }

    /*** label ***/
    private AppCompatTextView param_setting_spinner_label;
    /*** 下拉控件1 ***/
    private MySpinnerView spinner_1;
    /*** 下拉控件2 ***/
    private MySpinnerView spinner_2;
    /*** 下拉控件3 ***/
    private MySpinnerView spinner_3;
    /*** 下拉控件4 ***/
    private MySpinnerView spinner_4;
    /*** 下拉控件5 ***/
    private MySpinnerView spinner_5;

    private ArrayList<MySpinnerView> paramListView = new ArrayList<>();

    private void init(Context context) {
        setClipChildren(false);
        LayoutInflater.from(context).inflate(R.layout.view_param_setting_spinner, this, true);
        this.param_setting_spinner_label = findViewById(R.id.param_setting_spinner_label);
        this.spinner_1 = findViewById(R.id.spinner_1);
        this.spinner_2 = findViewById(R.id.spinner_2);
        this.spinner_3 = findViewById(R.id.spinner_3);
        this.spinner_4 = findViewById(R.id.spinner_4);
        this.spinner_5 = findViewById(R.id.spinner_5);

        this.paramListView.add(this.spinner_1);
        this.paramListView.add(this.spinner_2);
        this.paramListView.add(this.spinner_3);
        this.paramListView.add(this.spinner_4);
        this.paramListView.add(this.spinner_5);


    }

    public void setLabel(String label) {
        if (TextUtils.isEmpty(label)) {
            return;
        }
        this.param_setting_spinner_label.setText(label);
    }

    /**
     * 设置参数标识
     *
     * @param label
     * @param key
     */
    public void setParamTag(String label, String key) {
        int index = 0;
        for (MySpinnerView spinnerView : this.paramListView) {
            spinnerView.setParamTag(String.format("%s - 投口 - %s", label, index), String.format("%s_%s", key, index));
            index++;
        }
    }

    /**
     * 获取值
     *
     * @return
     */
    public ArrayList<HashMap<String, String>> getValue() {
        ArrayList<HashMap<String, String>> values = new ArrayList<>();
        for (MySpinnerView spinnerView : this.paramListView) {
            values.add(spinnerView.getValueMap());
        }
        return values;
    }
}
